package Util::UserDeliverContact::UserDeliverContact;
use Moose;
use namespace::autoclean;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub find_active_contacts_by_user {
	my ($self,$user_id) = @_;

	my @contacts = $self->schema->resultset('UserDeliverContact')->search({'user_id' => $user_id, 'effective_to' => '2999-01-01'},{});
	return \@contacts;
}

sub update_status{
	my ($self,$id,$status) = @_;

	my $contact = $self->schema->resultset('UserDeliverContact')->find($id);
	$contact->update({
		is_last_used => $status
	});
	return $contact
}

sub expire_contact{
	my ($self,$id) = @_;

	my $contact = $self->schema->resultset('UserDeliverContact')->find($id);
	$contact->update({
		effective_to => `date` 
	});
	return $contact
}

sub update_contact{
	my ($self,$id,$name,$address,$phone) = @_;
	my $contact = $self->schema->resultset('UserDeliverContact')->find($id);
	$contact->update({
		user_name => $name,
		user_address => $address,
		user_cellphone => $phone
	});
	return $contact
}

sub create {
        my ($self,$user_id ,$is_last_used,$effective_to,$user_name,$user_address,$user_cellphone) = @_;
        my $contact = $self->schema->resultset('UserDeliverContact')->create({
                        user_id => $user_id,
                        is_last_used => $is_last_used,
                        effective_to => $effective_to,
			user_name => $user_name,
			user_address => $user_address,
			user_cellphone => $user_cellphone
                        });
        return $contact;
}

sub find {
	my ($self,$id) = @_;

	my $contacts = $self->schema->resultset('UserDeliverContact')->search({'id' => $id},{})->first();
	return $contacts;
}


__PACKAGE__->meta->make_immutable;

1;
